package com.lovo.utils;

import com.lovo.enums.UserCodeEnum;
import com.lovo.vo.ResultVO;

import java.lang.reflect.InvocationTargetException;

/**
 * @title 表现层结果集对象工厂
 * @author: luo
 * @date: 2020/11/17 20:33
 */
public class ResultFactory {

    /**
     *携带数据的成功构造
     * @param data 携带的数据
     * @return
     */
    public static ResultVO success(Object data){
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode(1000);
        resultVO.setMsg("操作成功");
        resultVO.setData(data);
        return resultVO;
    }

    /**
     *不携带数据的成功构造
     * @return
     */
    public static ResultVO success(){
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode(1000);
        resultVO.setMsg("操作成功");
        return resultVO;
    }

    /**
     * 失败时返回的构造器
     * @param codeEnum 状态枚举
     * @return
     */
    public static ResultVO fail(Object codeEnum){
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(false);
        Integer code = null;
        String msg = null;
        try {
            code = (Integer) codeEnum.getClass().getMethod("getCode").invoke(codeEnum);
            msg = (String) codeEnum.getClass().getMethod("getMsg").invoke(codeEnum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultVO.setCode(code);
        resultVO.setMsg(msg);
        return resultVO;
    }

}
